#include "Proxy.h"

bool is_element_in_vector(vector<string> v, string element) {
    vector<string>::iterator it;
    it = find(v.begin(), v.end(), element);
    if (it != v.end()) {
        return true;
    }
    else {
        return false;
    }
}

bool Proxy::Access(string animal) {
    if (is_element_in_vector(animalType, animal))
        return 1;
    else
        return 0;
}

int Proxy::generate(std::string animal)
{
    if (Access(animal)) {
        if (luckyNum == NULL) {
            luckyNum = make_unique<LuckyNumber>();
            return luckyNum->generate(animal);
        }
        else {
            if (cache.find(animal) != cache.end())
                cache[animal] = luckyNum->generate(animal);
            return cache[animal];
        }
    }
    else
        return 0;
}
void Proxy::insertBlack(string animal) {
    BlackList.push_back(animal);
    cout << "Insert it successfully" << endl;
}

bool Proxy::checkBlack(string animal) {
    if (is_element_in_vector(BlackList, animal))
        return 1;
    else
        return 0;
}

void Proxy::delBlack(string animal) {
    for (vector<string>::iterator iter = BlackList.begin(); iter != BlackList.end(); )
    {
        if (*iter == animal)
        {
            iter = BlackList.erase(iter);
            continue;
        }
        iter++;
    }
    cout << "Delete it successfully" << endl;
}